﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.MainModule.Entities;
using Infraestructure.CrossCutting.IoC;
using Infraestructure.CrossCutting.Logging;
using DistributedServices.Core.ErrorHandlers;
using Application.MainModule.IdentitiesManagement;
using System.ServiceModel;

namespace DistributedServices.MainModule
{
	public partial class MainModuleService
	{
        public List<Identity> GetIdentityByUserId(Guid userId)
        {
            try
            {
                IIdentityManagementService identityService = IoCFactory.Instance.CurrentContainer.Resolve<IIdentityManagementService>();

                return identityService.FindIdentityByUserId(userId);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public Identity GetIdentityById(Guid identityId)
        {
            try
            {
                IIdentityManagementService identityService = IoCFactory.Instance.CurrentContainer.Resolve<IIdentityManagementService>();

                return identityService.FindIdentityById(identityId);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public void ChangeIdentity(Identity identity)
        {
            try
            {
                IIdentityManagementService identityService = IoCFactory.Instance.CurrentContainer.Resolve<IIdentityManagementService>();
                identityService.ChangeIdentity(identity);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }

        }

        public void RemoveIdentity(Identity identity)
        {
            try
            {
                IIdentityManagementService identityService = IoCFactory.Instance.CurrentContainer.Resolve<IIdentityManagementService>();
                identityService.RemoveIdentity(identity);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public void AddIdentity(Identity identity)
        {
            try
            {
                IIdentityManagementService identityService = IoCFactory.Instance.CurrentContainer.Resolve<IIdentityManagementService>();
                identityService.AddIdentity(identity);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

	}
}
